﻿<%@ WebHandler Language="C#" Class="PdmSendMesXmlReport" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Net;
using ReportBusiness;

/**
 * Description: PDM集成数量查询报表
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2024.11.28
 **/

public class PdmSendMesXmlReport : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportExcel":
                        exportExcel(jObject);//导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    Result getData(JObject jObject)
    {
        string sql = getSearchSql(jObject);
        DataTable dt = dal.GetDataTable(sql);
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        Result r = new Result();
        r.data = dt;
        r.total = Convert.ToInt32(dt.Rows.Count);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //查询方法
    string getSearchSql(JObject obj)
    {
        string sql = @"
SELECT TRUNC(il.createdate) createdate,COUNT(1) qty
FROM interface_log il
WHERE il.logtype LIKE '%PDMTOMES%' AND il.logmessage LIKE '接收到xml%'";
        //创建时间
        if (obj["selectdate"] != null && obj["selectdate"].ToString() != "")
        {
            JArray arr = JArray.Parse(obj["selectdate"].ToString());
            sql += string.Format(" AND il.createdate >= to_date('{0} 00:00:00','yyyy-MM-dd hh24:mi:ss')", arr[0].ToString());
            sql += string.Format(" AND il.createdate <= to_date('{0} 00:00:00','yyyy-MM-dd hh24:mi:ss')", arr[1].ToString());
        }

        sql += @"
GROUP BY TRUNC(il.createdate)
ORDER BY TRUNC(il.createdate) DESC";
        return sql;
    }

    void exportExcel(JObject jObject)
    {
        string sql = getSearchSql(jObject);
        DataTable dt = dal.GetDataTable(sql);
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        dt.Columns["createdate"].Caption = "日期";
        dt.Columns["qty"].Caption = "XML处理数量";
        ExportHelper.ExportExcel(dt, "PDM集成数量查询报表");
    }
}